package net.zjitc.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by dell on 2017/11/18.
 */
@WebFilter(filterName = "AdminFilter",urlPatterns = {"/admin/user/list.jsp","/admin/category/list.jsp","/admin/categorysecond/list.jsp","/admin/product/list.jsp"},dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class AdminFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request= (HttpServletRequest) req;
        HttpSession session=request.getSession();
        Object admin=session.getAttribute("admin");

        if(admin==null)
        {
            req.getRequestDispatcher("/admin/login.jsp").forward(req,resp);
        }
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
